package com.lagou.homework132;

/**
 * 编程获取两个指定字符串中的最大相同子串。
 * 如： s1="asdafghjka", s2="aaasdfg" 他们的最大子串为"asd"
 */
public class FindMaxSubString {
    public static void main(String[] args) {

        String str1 = "asdafghjka";
        String str2 = "aaasdfg";

        String max = (str1.length() > str2.length()) ? str1 : str2;
        String min = (str1.length() < str2.length()) ? str1 : str2;
        String result = "";

        //将长度较短的字符串取长度递减的子串，与长的字符串比较
        outer:
        for (int i = 0; i < min.length(); i++) {
            for (int j = 0, k = min.length() - i; k < min.length() + 1; j++, k++) {
                String sub = min.substring(j, k);
                if (max.contains(sub)) {
                    result = sub;
                    break outer;
                }
            }
        }

        System.out.println("长字符串：" + max);
        System.out.println("短字符串：" + min);
        System.out.println("最大子串是：" + result);
    }
}
